import { requestPUT, requestGET } from '../../utils/service'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    date: '',//检测日期
    okToPay: false,//是否填完信息可以支付了
    payWay: "2",// 支付方式
    showCheckInfoModal: false,
    showInputDHMModal: false,
    packageInfo: {} as any,
    personInfo: wx.getStorageSync('userInfo'),
    CDK: '',
    machineName: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData(
      { packageInfo: options }
    )
    const date = new Date();
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    this.setData(
      { date: `${year}-${month}-${day}` }
    )
    this.confirmInfo()
  },

  // 选择日期
  bindDateChange(e: any) {
    this.setData({ date: e.detail.value, okToPay: true })
  },

  // 点击《去支付》 弹出cdk输入
  confirmInfo() {
    // this.setData({ showInputDHMModal: true })
    this.setData({ showCheckInfoModal: true })
  },

  //选择支付方式
  choosePayWay(e: any) {
    this.setData({ payWay: e.currentTarget.dataset.type })
  },

  closeCheckInfo() {
    //this.setData({ showCheckInfoModal: false })
    wx.navigateBack({
      delta: 1 // 返回的页面层级，1表示返回上一页
    });
  },

  // 确认支付 (请求接口去预约)
  onOkToPay() {
    // if (!this.data.CDK) {
    //   wx.showToast({
    //     title: '请输入兑换码',
    //     icon: 'error',
    //     duration: 2000
    //   })
    //   return
    // }
    const params = {
      "physicalExaminationPackageId": this.data.packageInfo.physicalExaminationPackageId,
      "reservationDate": this.data.date,
      // "physicalExaminationCdkey": this.data.CDK
    }
    requestPUT("reservation/reservePhysicalExaminationPackage", params)
      .then((res: any) => {
        console.log(res)
        if (res.data.code == '200') {
          this.setData({ showCheckInfoModal: false })
          wx.navigateTo({
            url: '/pages/reservationOk/index?date=' + this.data.date + '&&physicalExaminationLocation=' + this.data.packageInfo.physicalExaminationLocation,
          })
          // wx.showToast({
          //   title: '预约成功',
          //   icon: 'success',
          //   duration: 2000,
          //   success: function () {
          //     setTimeout(function () {
          //       // 返回上层界面
          //       wx.navigateBack({
          //         delta: 1
          //       })
          //     }, 2000)
          //   }
          // })
        } else {
          wx.showToast({
            title: '预约失败',
            icon: 'error',
            duration: 2000
          })
        }
      })
  },

  // 取消输入兑换码
  cancleInputDHM() {
    this.setData({ showInputDHMModal: false })
  },

  // 输入CDK
  inputCDK(e: any) {
    this.setData({
      CDK: e.detail.value
    })
  },

  // 确认输入兑换码 进行校验
  confirmInputDHM() {
    const params = {
      "physicalExaminationPackageId": this.data.packageInfo.physicalExaminationPackageId,
      "reservationDate": this.data.date,
      "physicalExaminationCdkey": this.data.CDK
    }
    requestGET("reservation/validate", params)
      .then((res: any) => {
        console.log(res)
        if (res.data.code == '200') {
          this.setData({
            machineName: res.data.data
          })
          this.setData({ showCheckInfoModal: true, showInputDHMModal: false })
        } else {
          wx.showToast({
            title: '兑换码不存在',
            icon: 'error',
            duration: 2000
          })
        }
      })
  },
})